﻿using System.Collections.Generic;
using SDT.ServiceSystem;
using System.Diagnostics;

namespace SDT.CrawlSystem.Servers
{
    /// <summary>
    /// Written by TungNX
    /// </summary>
    public sealed class UCContainer
    {
        // Workers list
        private IDictionary<string, IWorkerThread> Workers = new Dictionary<string, IWorkerThread>();

        #region Singleton pattern
        private static UCContainer instance = new UCContainer();
        public static UCContainer Instance
        {
            get
            {
                lock (instance)
                {
                    foreach (var f in Global.Instance.DataFiles)
                    {
                        if (!instance.Workers.ContainsKey(f))
                            instance.Workers.Add(f, new UrlCrawler(f, instance));
                    }
                    return instance;
                }
            }
        }

        private UCContainer()
        {
        }
        #endregion

        public void StartAll()
        {
            lock (Workers)
                foreach (var w in Workers.Values)
                {
                    w.StartWorking();              
                }
        }

        public bool StopAll()
        {
            lock (Workers)
                foreach (var w in Workers.Values)
                    if (!w.StopWorking())
                        return false;

            return true;
        }
    }
}